﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    class MoveCommand : SpeechCommand
    {
        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            Choices ch = new Choices();
            for (int i = 1; i <= 50; i++)
                ch.Add(new SemanticResultValue(i.ToString(), i));

            GrammarBuilder gb = new GrammarBuilder(new Choices("move up", "move right", "move down", "move left"));
            gb.Append(new SemanticResultKey("number", ch));
            return gb;
        }

        public override string Name
        {
            get { return "Move command"; }
        }

        public override string Description
        {
            get { return "Move command. EX: Move down 5"; }
        }

        internal override bool IsUndoable
        {
            get { return false; }
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            if (spokenForm.StartsWith("move up "))
            {
                for (int i = 1; i <= 50; i++)
                {
                    if (spokenForm == "move up " + i)
                        return true;
                }
            }

            if (spokenForm.StartsWith("move right "))
            {
                for (int i = 1; i <= 50; i++)
                {
                    if (spokenForm == "move right " + i)
                        return true;
                }
            }

            if (spokenForm.StartsWith("move down "))
            {
                for (int i = 1; i <= 50; i++)
                {
                    if (spokenForm == "move down " + i)
                        return true;
                }
            }

            if (spokenForm.StartsWith("move left "))
            {
                for (int i = 1; i <= 50; i++)
                {
                    if (spokenForm == "move left " + i)
                        return true;
                }
            }

            return false;
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            try
            {
                if (recognizedText.StartsWith("move up"))
                {
                    for (int i = 0; i < (int)semValue["number"].Value; i++)
                        callingMode.CurrentOutputHandler.SendKeys("{UP}");
                }

                if (recognizedText.StartsWith("move right"))
                {
                    for (int i = 0; i < (int)semValue["number"].Value; i++)
                        callingMode.CurrentOutputHandler.SendKeys("{RIGHT}");
                }

                if (recognizedText.StartsWith("move down"))
                {
                    for (int i = 0; i < (int)semValue["number"].Value; i++)
                        callingMode.CurrentOutputHandler.SendKeys("{DOWN}");
                }

                if (recognizedText.StartsWith("move left"))
                {
                    for (int i = 0; i < (int)semValue["number"].Value; i++)
                        callingMode.CurrentOutputHandler.SendKeys("{LEFT}");
                }

                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
